/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.DepartmentTreeBean;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.domain.GridNode;
import com.koron.risk.service.api.GridNodeService;
import com.koron.risk.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "GridNode", description = "网格管理")
@RestController
@RequestMapping("/GridNode")
public class GridNodeController {

    @Autowired
    private GridNodeService service;

    @ApiOperation("新增网格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "网格名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serial", value = "排序号" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "父id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyStaffCode", value = "第一责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffCode", value = "直接责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyStaffName", value = "第一责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffName", value = "直接责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeader", value = "安全分管领导code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeaderName", value = "安全分管领导名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficer", value = "安全员code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficerName", value = "安全员名称" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/save.htm", method = {RequestMethod.POST})
    @Log(title = "新增网格", businessModule = "网格管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore GridNode bean) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.save(user, bean);
    }

    @ApiOperation("网格修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "网格名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serial", value = "排序号" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "subnumber", value = "子节点数量" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "safetyStaffCode", value = "第一责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffCode", value = "直接责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyStaffName", value = "第一责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffName", value = "直接责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeader", value = "安全分管领导code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeaderName", value = "安全分管领导名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficer", value = "安全员code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficerName", value = "安全员名称" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/update.htm", method = {RequestMethod.POST})
    @Log(title = "网格修改", businessModule = "网格管理", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore GridNode bean) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.update(user, bean);
    }

    @ApiOperation("网格删除")
    @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    @Log(title = "网格删除", businessModule = "网格管理", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.delete(id);
    }

    @ApiOperation("网格详情")
    @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/details.htm", method = {RequestMethod.GET})
    public MessageBean<?> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.details(id);
    }

    @ApiOperation("下级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "recursive", value = "是否返回当前节点 0是 1否" , paramType = "query", dataType = "Integer")
    })
    @RequestMapping(value = "/childList.htm", method = {RequestMethod.GET})
    public MessageBean<?> childList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,String id, Integer recursive) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.childList(id,recursive);
        //return service.childList2("776B361BFFAC4F00BBF459CD1303A8B2",id, recursive);
    }

    @ApiOperation("下级列表，速度更快一点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "当前水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "recursive", value = "是否返回当前节点 0是 1否" , paramType = "query", dataType = "Integer")
    })
    @RequestMapping(value = "/childList2.htm", method = {RequestMethod.GET})
    public MessageBean<?> childList2(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,String corpCode, String id, Integer recursive) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.childList2(corpCode,id, recursive);
    }

    @ApiOperation("所有列表")
    @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.list(corpCode);
    }

    @ApiOperation("网格配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "nodeId", value = "节点id(添加在该节点下)" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/batchSave.htm", method = {RequestMethod.POST})
    public MessageBean<?> batchSave(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, String nodeId, @RequestBody List<GridNode> beanList) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.batchSave(user, corpCode, nodeId, beanList);
    }


    @ApiOperation("设置安全负责人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptCode", value = "网格code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyStaffCode", value = "第一责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffCode", value = "直接责任人code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyStaffName", value = "第一责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessStaffName", value = "直接责任人名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeader", value = "安全分管领导code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyLeaderName", value = "安全分管领导名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficer", value = "安全员code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "safetyOfficerName", value = "安全员名称" , paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/setStaff.htm", method = {RequestMethod.POST})
    @Log(title = "部门设置安全负责人", businessModule = "网格管理", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> setStaff(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String deptCode, String safetyStaffCode, String safetyStaffName,
                                   String businessStaffCode, String businessStaffName,String safetyLeader, String safetyLeaderName, String safetyOfficer, String safetyOfficerName) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.setStaff(deptCode, safetyStaffCode, safetyStaffName, businessStaffCode, businessStaffName,safetyLeader,safetyLeaderName,safetyOfficer,safetyOfficerName);
    }

    @ApiOperation("上级网格负责人")
    @ApiImplicitParam(name = "code", value = "code" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/gridStaff.htm", method = {RequestMethod.GET})
    public MessageBean<?> gridStaff(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        Map<String, String> map = service.gridStaff(code);
        MessageBean<Map> msg = MessageBean.create(BusinessCode.MESSAGE_CODE_SUCCESS, BusinessCode.MESSAGE_DESCRIPTION_SUCCESS, Map.class);
        msg.setData(map);
        return msg;
    }

    @ApiOperation("离本级网格最近的负责人")
    @ApiImplicitParam(name = "code", value = "code" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/getGridStaff.htm", method = {RequestMethod.GET})
    public MessageBean<?> getGridStaff(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.getGridStaff(code);
    }


    @ApiOperation("根据网格名称模糊查询网格平铺树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "网格名称" , paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/getGridListByName.htm", method = { RequestMethod.GET })
    public MessageBean<?> getGridListByName(@RequestParam(required = true) final String corpCode,@RequestParam(required = false) final String name){
        return  service.appGetGridByName(corpCode,name);
    }
//
//    @ApiOperation("查询网格树")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
//    })
//    @RequestMapping(value = "/gridTree.htm", method = { RequestMethod.GET })
//    public MessageBean<?> gridTree(String corpCode){
//      return service.gridTree(corpCode);
//    }

    @ApiOperation("测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/getAllNodes.htm", method = { RequestMethod.GET })
    public MessageBean<Map> getAllNodes(String corpCode){
        return service.getAllNodes(corpCode);
    }

    @ApiOperation("获取网格所属单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gridCode", value = "网格code", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/getCorpInfo.htm", method = RequestMethod.GET)
    public MessageBean<DepartmentTreeBean> getCorpInfo(String gridCode) {
        MessageBean<DepartmentTreeBean> msg = MessageBean.create(0, "SUCCESS", DepartmentTreeBean.class);
        if (StringUtils.isEmpty(gridCode)){
            msg.setCode(-1);
            msg.setDescription("必要参数gridCode不能为空");
            return msg;
        }
        DepartmentTreeBean treeBean = ADOConnection.runTask(factory -> service.getCorpInfo(factory, gridCode), DepartmentTreeBean.class);
        if (null !=treeBean){
            msg.setData(treeBean);
        }else {
            msg.setCode(-1);
            msg.setDescription("获取所属单位失败");
        }
        return msg;
    }
}
